﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace 谁与争锋可视化设计
{
    class PageAnalysis
    {
        //所有题目的题号
        private String[] m_allAcProblemNum;

        //计算所有ac的题号
        public void GetProblemNums(string baseMessagePage)
        {
            //ac做题数的匹配字符串
            string pattern = "[0-9]{4},?[1-9][0-9]*,[0-9]*";

            MatchCollection matches = Regex.Matches(baseMessagePage, pattern);
            string[] allAcProblemNum = new string[matches.Count];
            int i = 0;

            foreach (Match match in matches)
            {
                allAcProblemNum[i] = match.ToString().Substring(0, 4);
                i++;
            }

            m_allAcProblemNum = allAcProblemNum;          
        }

        //提取题目信息
        public void ExtractMessages(string acProblemPage, User user, int questionSum)
        {
            //得到用户题库的引用
            User.question[] questionBank = user.QuestionBank = new User.question[m_allAcProblemNum.Length];
            user.m_questionSum = questionSum;

            //键值--题号,值--索引
            Dictionary<int, int> dictionary = new Dictionary<int,int>();

            //存储题号
            for (int i = 0; i < m_allAcProblemNum.Length; i++)
            {
                questionBank[i].proID = int.Parse(m_allAcProblemNum[i]);
                questionBank[i].submitTime = new List<DateTime>();
                questionBank[i].language = new List<string>();
                dictionary.Add(questionBank[i].proID, i);
            }
            
            //匹配结果包括做题时间和题号,格式:时间+题号
            string datePattern = "[0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}.{79}[0-9]{4}";
            MatchCollection dateMatches = Regex.Matches(acProblemPage, datePattern);

            //匹配后的格式:题号+使用语言
            string languagePattern = @"(?<=<td>)(C\+\+|G\+\+|C|Java|GCC|Pascal)";
            MatchCollection languageMatches = Regex.Matches(acProblemPage, languagePattern);

            for (int i = 0; i < dateMatches.Count; i++)
            {
                Match dateMatch = dateMatches[i];
                string submitTime = dateMatch.ToString().Substring(0, 19);
                string proID = dateMatch.ToString().Substring(98);
                Match languageMatch = languageMatches[i];
                string language = languageMatch.ToString();

                int index;
                dictionary.TryGetValue(int.Parse(proID), out index);

                questionBank[index].submitTime.Add(DateTime.Parse(submitTime));
                questionBank[index].language.Add(language);
          
            }
          
            
        }

    }
}
